//
//  PreTransactionCell.swift
//  Share
//
//  Created by admin on 2020/12/7.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI

struct PreTransactionBuyCell: View {
    
    var store: Store
    var share: Share
    @State var percent: Float = 0
    
    var body: some View {
        let storein = store.price * (Float( share.ratioIn) ?? 0.95)
        let num = (storein==0) ? 0 : (Int(2000/(storein*100))+1)*100
        
        HStack(alignment: .center){
         
            VStack{
                Progress( percent: self.percent*100)
                    .frame(width: 44, height: 44, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                Text("买价：\(storein ,specifier: "%0.3f")")
                    .font(.system(size: 10))
                    .foregroundColor(storein>0 ? .red_1 : .blue_2)
            }
            
            VStack(alignment: .leading){
                
                Text("数量：\(num)")
                Text("合计\(storein * Float(num) , specifier: "%0.2f")")
            }
            .font(.system(size: 10))
            .foregroundColor(.grey_2)
        }
        
        .padding(.all)
        .background(Color("background3"))
        .cornerRadius(4)
        .shadow(color: Color.shadow_1.opacity(0.5), radius: 2, x: 0, y: 0)
        .onAppear{
            
            self.percent = storein/(Float(share.price) ?? 0.0001)
        }

    }
    
}

struct PreTransactionBuyCell_Previews: PreviewProvider {
    static var previews: some View {
        PreTransactionBuyCell(store: Store._stores[0],
                              share:Share._shares[0],
                              percent: 0.8
        )
        
        .frame(width: 200,  alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
        .background(Color("background2"))
        
    }
}
